"""
Convert HTML API documentation to Markdown format
"""
from bs4 import BeautifulSoup
import html2text
import re
import os

def convert_html_to_markdown(html_file, output_file):
    """
    Convert HTML file to Markdown

    Args:
        html_file: Path to input HTML file
        output_file: Path to output Markdown file
    """
    # Read HTML file
    with open(html_file, 'r', encoding='utf-8') as f:
        html_content = f.read()

    # Use BeautifulSoup to clean up the HTML
    soup = BeautifulSoup(html_content, 'html.parser')

    # Remove unnecessary elements
    for element in soup(['script', 'style', 'svg', 'noscript', 'header', 'footer', 'nav']):
        element.decompose()

    # Try to find the main content
    main = soup.find('main') or soup.find('article') or soup.find('div', id='docArticle') or soup.body

    if not main:
        print("Warning: Using full body content")
        main = soup

    # Convert to string
    cleaned_html = str(main)

    # Use html2text to convert
    h = html2text.HTML2Text()
    h.ignore_links = False
    h.ignore_images = False
    h.ignore_emphasis = False
    h.body_width = 0  # No line wrapping
    h.unicode_snob = True
    h.ignore_tables = False
    h.skip_internal_links = True

    markdown_content = h.handle(cleaned_html)

    # Clean up the markdown
    # Remove excessive newlines
    markdown_content = re.sub(r'\n{4,}', '\n\n', markdown_content)

    # Add header
    header = "# Docmee API Documentation\n\n"
    header += "Source: https://open.docmee.cn/open/doc/api\n\n"
    header += "---\n\n"

    markdown_content = header + markdown_content

    # Write to output file
    with open(output_file, 'w', encoding='utf-8') as f:
        f.write(markdown_content)

    print(f"Conversion completed: {output_file}")
    print(f"Output file size: {os.path.getsize(output_file)} bytes")

    # Show first few lines
    lines = markdown_content.split('\n')[:30]
    print(f"\nFirst 30 lines preview:")
    for i, line in enumerate(lines, 1):
        print(f"{i}: {line[:100]}")

if __name__ == "__main__":
    html_file = r"C:\project\py\teach_workflow\docs\view-source_https___open.docmee.cn_open_doc_api.html"
    output_file = r"C:\project\py\teach_workflow\docs\docmee_api_documentation.md"

    convert_html_to_markdown(html_file, output_file)
